unit Unit45; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  DBGrids, Unit1, Menus, Unit46, Unit47, LR_DBSet, LR_Class;

type

  { TForm45 }

  TForm45 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow();
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
    procedure ordenar_grid();
  private
    nombre : string;
  public
    { public declarations }
  end; 

var
  Form45: TForm45; 

implementation

{ TForm45 }

procedure TForm45.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from funcionario where fnc_nombre ilike ''%' + nombre
    + '%'' order by fnc_nombre';

  SQLQuery1.Open;
  
  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre';
  DBGrid1.Columns[2].Title.Caption := 'Descripción';


  DBGrid1.Columns[0].Width := 60;
  DBGrid1.Columns[1].Width := 150;
  DBGrid1.Columns[2].Width := 225;
end;

procedure TForm45.FormShow();
begin
  nombre := '%';
  
  ordenar_grid();
end;

procedure TForm45.MenuItem10Click(Sender: TObject);
begin
  frReport1.LoadFromFile('listado_funcionarios.lrf');
  frReport1.ShowReport;
end;

procedure TForm45.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm45.DBGrid1CellClick(Column: TColumn);
begin
  Form47.codigo := SQLQuery1.FieldByName('fnc_cdg').AsString;
  
  Form47.ShowModal;
  
  ordenar_grid();
end;

procedure TForm45.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
end;

procedure TForm45.MenuItem2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm45.MenuItem4Click(Sender: TObject);
begin
  Form46.ShowModal;
  ordenar_grid();
end;

procedure TForm45.MenuItem5Click(Sender: TObject);
var
  codigo : string;
  QueryString: string;
  
begin
  codigo := InputBox('Baja de Funcionario', 'Ingrese el codigo de funcionari oa eliminar:', '');
  
  QueryString := 'delete from funcionario where fnc_cdg = ' + codigo;
  
  Form1.PQConnection1.ExecuteDirect('begin work;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('commit work;');
  
  ShowMessage('BAJA EXITOSA');
  
  ordenar_grid();
end;

procedure TForm45.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click en el funcionario a modificar...');
end;

procedure TForm45.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtro por Nombre', 'Ingrese parte del nombre:', '');
  ordenar_grid();
end;

procedure TForm45.MenuItem9Click(Sender: TObject);
begin
  FormShow;
end;

initialization
  {$I unit45.lrs}

end.

